import uuid
from functools import wraps

from flask import Flask, render_template, flash, redirect, url_for, request, abort, session, g

from models import db, User

# 引入蓝图
from accounts.views import accounts
from mall.views import mall

app = Flask(__name__)
app.config.from_object('conf.Config')
# 使用ORM
db.init_app(app)
# 注册蓝图
app.register_blueprint(accounts, url_prefix='/account')
app.register_blueprint(mall, url_prefix='/mall')

def login_require(view_func):
    """登录验证"""
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        userid = session.get('userid', None)
        if userid:
            flash('请登录', 'danger')
            return redirect(url_for('accounts.login'))
        return view_func(*args, *kwargs)
    return wrapper

@app.route('/')
def index():
    """首页"""
    return render_template('index.html')

@app.before_request
def before_request():
    """ 如果有用户的话，设置到全局对象g """
    userid = session.get('userid', None)
    if userid:
        user = User.query.get(userid)
        print(user)
        g.user = user


print(app.url_map)

# if __name__ == '__main__':
#     app.run()
